<template>
  <div class="chapter">
    <div :class="['heads', show ? 'con' : '']" v-show="flg">
      <div class="fh" @click="$router.go(-1)">
        <span class="iconfont icon-arrow-left-bold"></span> 返回
      </div>
      <div class="shuJ">
        <span class="iconfont icon-shujia"></span>
        <span class="iconfont icon-wode" @click.stop="goUserInfo"></span>
      </div>
    </div>
    <div
      :class="['catalogue', show ? 'con' : '']"
      v-show="flg"
      @click="goBooksItem"
    >
      <div class="iconfont icon-fankaishu"></div>
      <div class="n">目录</div>
    </div>
    <div :class="['bottom', show ? 'con' : '']" v-show="flg">
      <div class="syy" @click="lastChapter">
        <div class="iconfont icon-zuofanye"></div>
        <div>上一章</div>
      </div>
      <van-popover
        v-model="showPopover"
        trigger="click"
        placement="top"
        class="textT"
        get-container=".chapter"
      >
        <div class="box" style="padding: 10px">
          <button
            class="addT"
            style="margin: 0 8px; padding: 0 10px"
            @click="txtSize--"
          >
            T-
          </button>
          <button
            class="editT"
            style="margin: 0 8px; padding: 0 10px"
            @click="txtSize++"
          >
            T+
          </button>
        </div>
        <template #reference>
          <div class="syy textBtn">
            <div class="iconfont icon-ziti"></div>
            <div>字体</div>
          </div>
        </template>
      </van-popover>
      <div class="syy" @click="atNight">
        <div :class="['iconfont', show ? 'icon-rijian' : 'icon-yejian']"></div>
        <div>{{ show ? "白天" : "夜间" }}</div>
      </div>
      <van-popover
        v-model="showColor"
        trigger="click"
        class="textT color"
        placement="top-end"
        get-container=".chapter"
      >
        <div class="box">
          <div class="item" @click="textBg = 'text-bg1'">
            <div class="img text-bg1"></div>
            <div class="name">羊皮纸</div>
          </div>
          <div class="item" @click="textBg = 'text-bg2'">
            <div class="img text-bg2"></div>
            <div class="name">淡雅白</div>
          </div>
          <div class="item" @click="textBg = 'text-bg3'">
            <div class="img text-bg3"></div>
            <div class="name">冰爽蓝</div>
          </div>
          <div class="item" @click="textBg = 'text-bg4'">
            <div class="img text-bg4"></div>
            <div class="name">浪漫粉</div>
          </div>
          <div class="item" @click="textBg = 'text-bg5'">
            <div class="img text-bg5"></div>
            <div class="name">护眼绿</div>
          </div>
        </div>
        <div class="fy">
          <div class="zy" @click="pageTurning = true">左右翻页</div>
          <div class="zy" @click="fullRad">上下翻页</div>
        </div>
        <template #reference>
          <div class="syy textBtn">
            <div class="iconfont icon--huangguan"></div>
            <div>背景</div>
          </div>
        </template>
      </van-popover>

      <div class="syy" @click="nextChapter">
        <div class="iconfont icon-youfanye"></div>
        <div>下一章</div>
      </div>
    </div>
    <div
      :class="['content', show ? 'night' : '', textBg, pageTurning ? 'zy' : '']"
      :style="'font-size:' + txtSize + 'px'"
    >
      <div class="bg" v-show="show"></div>
      <div class="black" @click.stop="black"></div>
      <div class="nextY" @click.stop="nextY" v-if="pageTurning"></div>
      <div class="lastY" @click.stop="lastY" v-if="pageTurning"></div>
      <article ref="art">
        <div v-html="text" v-if="text" ref="getWidth"></div>
      </article>
      <div class="no-txt" v-if="login">
        <div class="t">此章节为付费章节请充值购买</div>
        <div class="bnt">
          <van-button
            type="danger"
            size="small"
            @click.stop=""
            v-if="!token"
            to="/login"
            >登录</van-button
          >

          <van-button
            type="danger"
            size="small"
            @click.stop="$router.push({ path: '/congzhi' })"
            v-if="token"
            >充值</van-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getBookData } from "../api/chapter";
export default {
  data() {
    return {
      flg: false,
      text: null,
      showPopover: false,
      show: false,
      showColor: false,
      textBg: null,
      txtSize: 16,
      token: "",
      login: "",
      pages: 0,
      PageCount: null,
      id: null,
      zId: null,
      pageTurning: false,
      nextId: null,
      lastChapterItem: null,
    };
  },
  methods: {
    nextY() {
      console.log("下一页");
      this.pages = ++this.pages;
      if (this.pages > this.PageCount) {
        this.nextChapter();
        // this.pages=0;
        return;
      }
    },
    lastY() {
      console.log("上一页");
      this.pages = --this.pages;
      if (this.pages < 0) {
        this.lastChapter();
        // this.pages=0;
        return;
      }
    },
    fullRad() {
      this.pageTurning = false;
      this.pages = 0;
    },
    atNight() {
      this.show = !this.show;
    },
    black() {
      console.log("sss");
      this.flg = !this.flg;
    },
    goBooksItem() {
      this.$router.push({ path: "/bookDetails/booksItem" });
    },
    lastChapter() {
      let list = this.$store.state.booksList;
      // let source_uuid = this.$store.state.id;
      let index = list.findIndex((item) => item.uuid == this.zId);
      index = --index >= 0 ? index : 0;
      this.zId = list[index].uuid;
      this.getData();
    },
    nextChapter() {
      this.zId = this.nextId;
      this.text = this.lastChapterItem;
      this.pages=0;
       document.body.scrollTop = document.documentElement.scrollTop = 0;
    },
    getData() {
      let source_uuid = this.id;
      let content_uuid = this.zId;
      getBookData({
        source_uuid,
        content_uuid,
      }).then((data) => {
        console.log(data);
        if (!data.data) {
          this.login = true;
        }
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        this.pages = 0;
        this.text = data.data.content;
      });
    },
    goUserInfo() {
      this.$router.push({ path: "/userinfo" });
    },
    nextItem() {
      let list = this.$store.state.booksList;
      let index = list.findIndex((item) => item.uuid == this.zId);
      index = ++index < list.length ? index : list.length - 1;
      this.nextId = list[index].uuid;
      getBookData({
        source_uuid: this.id,
        content_uuid: this.nextId,
      }).then((data) => {
        console.log(data);
        if (!data.data) {
          this.login = true;
        }
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        this.pages = 0;
        this.lastChapterItem = data.data.content;
      });
    },
  },
  watch: {
    zId() {
      this.nextItem();
      // this.zId=this.nextId
    },
    text() {
      //   let width = document.body.offsetWidth;
      //  this.PageCount=Math.floor( this.$refs.widths.style.watch / width);
      this.$nextTick(function () {
        // DOM 现在更新了
        // `this` 绑定到当前实例
        // this.doSomethingElse()
        this.PageCount = Math.floor(
          this.$refs.getWidth.scrollHeight / window.screen.height
        );
        // console.log(width);
      });
    },
    pages() {
      let width =
        window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth;
      this.$refs.art.style.transform = `translateX(-${
        (width - 16) * this.pages
      }px)`;
      if (this.pages == 0) {
        this.$refs.art.style.transition = "0s";
      } else {
        this.$refs.art.style.transition = "0.4s";
      }
    },
  },
  created() {
    this.id = this.$store.state.id;
    this.zId = this.$store.state.zId;
    this.getData();
    this.token = window.localStorage.getItem("token");
    this.nextItem();
  },
  beforeDestroy() {
    let history = this.$store.state.history;
    let source_uuid = this.id;
    let content_uuid = this.zId;
    // console.log(source_uuid,content_uuid)
    if (history.length) {
      let index = history.findIndex((item) => item.source_uuid == source_uuid);
      if (index != -1) {
        history[index].content_uuid = content_uuid;
      } else {
        history.push({
          source_uuid,
          content_uuid,
        });
      }
    } else {
      history.push({
        source_uuid,
        content_uuid,
      });
    }
    this.$store.commit("setHistory", history);
  },
};
</script>
<style lang="less">
.chapter {
  position: relative;
  height: 100%;
  .text-bg1 {
    // background-color: rgb(230, 224, 207);
    background-image: url("https://static.zongheng.com/h5/images/bg_ypz.png");
  }
  .text-bg2 {
    // background-color: rgb(247, 245, 240);
    background-image: url("https://static.zongheng.com/h5/images/bg_dyb.png");
  }
  .text-bg3 {
    background-image: url("https://static.zongheng.com/h5/images/bg_bsl.gif");
  }
  .text-bg4 {
    background-color: #e0b6c4;
  }
  .text-bg5 {
    background-color: #a6bc9c;
  }
  .fy {
    display: flex;
    justify-content: space-around;
    margin-bottom: 15px;
    .zy {
      background-color: #ebedf0;
      color: rgb(102, 100, 100);
      font-size: 14px;
      padding: 3px 5px;
      border-radius: 4px;
    }
  }
  .box {
    display: flex;
    padding: 10px;
    .item {
      margin: 5px;
    }
    .img {
      width: 40px;
      height: 40px;
      // background-color: pink;
    }
    .name {
      font-size: 12px;
    }
  }
  .content {
    &.zy {
      height: 100vh;
      overflow: hidden;
    }
    padding: 0 16px;
    article {
      columns: calc(100vw - 32px) 1;
      column-gap: 16px;
      height: 100%;
      transition: 0.4s;
    }
    .m-mb {
    }
    .black {
      position: fixed;
      width: 200px;
      height: 100%;
      // background-color: pink;
      z-index: 1;
      left: 0;
      right: 0;
      margin: auto;
    }
    .nextY {
      position: fixed;
      width: 80px;
      height: 100%;
      right: 0;
      // background-color: pink;
      z-index: 1;
    }
    .lastY {
      position: fixed;
      width: 80px;
      height: 100%;
      // background-color: pink;
      left: 0;
      z-index: 1;
    }
    .no-txt {
      height: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      text-align: center;
      position: absolute;
      left: 0;
      right: 0;
      .bnt {
        z-index: 1;
        button {
          margin: 10px;
        }
      }
    }
    &.night {
      background-color: #333333;
      color: #c8c9cc;
    }
    .m-content {
      padding-top: 44px;
      h1 {
        text-align: center;
        margin-bottom: 20px;
      }
      // padding: 10px;
      p {
        text-indent: 2em;
        overflow-x: hidden;
        line-height: 2rem;
        // font-size: 20px;
      }
    }
    .title {
      font-weight: bold;
      text-align: center;
    }
  }
  .heads {
    position: fixed;
    top: 0;
    width: 100%;
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    color: #fff;
    background-color: #333;
    height: 44px;
    line-height: 44px;
    z-index: 2;
    .fh {
      padding-left: 10px;
    }
    .shuJ {
      padding-right: 10px;
      span {
        margin-left: 20px;
      }
    }
  }
  .catalogue {
    background: #333;
    position: fixed;
    color: #fff;
    z-index: 2;
    width: 40px;
    height: 40px;
    padding: 10px 2px;
    top: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    text-align: center;
    border-top-right-radius: 13px;
    border-bottom-right-radius: 13px;
    .n {
      font-size: 14px;
      margin-top: 5px;
    }
  }
  .bottom {
    position: fixed;
    z-index: 2;
    bottom: 0;
    width: 100%;
    height: 44px;
    display: flex;
    font-size: 14px;
    background-color: #333;
    align-items: center;
    text-align: center;
    color: #fff;
    .van-popover[data-popper-placement="top"] {
      width: 100px;
      height: 100px;
    }
    .textT {
      width: 20%;
      .van-col--4 {
        width: 20%;
      }
    }
    .syy {
      width: 20%;
    }
    .textBtn {
      width: auto;
    }
  }
  .con {
    color: #c8c9cc;
  }
}
</style>